import requests
import json
from .conversation import Conversation

def Req(conversation: Conversation) -> dict:
    # 服务器的地址
    url = "http://127.0.0.1:7002/predict"

    # 将 JSON 数据转换为字符串
    json_data = conversation.to_json()

    images_list = conversation.get_image_path_list()
    print(len(images_list))
    has_images = len(images_list) > 0
    print(has_images)

    data = {
        "json" : (None, json_data)
    }

    if has_images:
        files_list = [
            ("images", open(image_path, "rb")) for image_path in images_list
        ]
        print(files_list)

    # 发送请求
    if has_images:
        response = requests.post(url, data=data, files=files_list)
    else:
        response = requests.post(url, data=data)

    # 打印返回的结果
    print(response.json())
